<template>
	<view class="container index">
		<!-- 头部导航 -->
		<nav-bar backState="2000" bgColor="#4c8aed" title="陪诊小程序" />
		<scroll-view
			class="list-scroll-content"
			:style="{height:`calc(100vh - ${titleBar}px)`}"
			scroll-y
			:refresher-triggered="!isLoad"
			:refresher-enabled="true"
			@refresherrefresh="loadData('refresh')"
			@scrolltolower="loadData"
		>
			<view class="pure_top"><h-icon class-name="ico_nav" src="nav_bg2" type="jpg" mode="heightFix"></h-icon></view>
			<swiper class="swiper"
				:indicator-dots="true"
				indicator-color="rgba(0, 0, 0, 0.5)"
				indicator-active-color="#15CC81"
				:autoplay="true"
				:interval="3000"
				:duration="500">
				<swiper-item v-for="(item,index) in bannerList" :key="index">
					<view class="swiper-item uni-bg-red">
						<image class="img" :src="item.imageUrl"></image>
					</view>
				</swiper-item>
			</swiper>
			<view class="index_banner">
				<view class="item">
					<image class="b_bg" src="/static/icon/ban1.png" />
					<view class="text" @click="goAccompany">一键陪诊</view>
				</view>
				<view class="item">
					<image class="b_bg" src="/static/icon/ban2.png" />
					<view class="text">智能导诊</view>
				</view>
			</view>
			<!-- 瀑布流导航 -->
			<view class="tit-bar"><text class="blod">附近医院</text></view>
			<!-- 空白页 -->
			<view v-if="isEmpty" class="empty" :style="{minHeight:`calc(100vh - 400rpx - ${titleBar}px)`}">
				<empty class="cont" tips="暂无数据" src="empty/await" />
			</view>
			<view v-else class="hospital-list">
				<view v-for="(item, index) in hospitalList" :key="index">
					<hospital-card :item="item" />
				</view>
			</view>
			<uni-load-more v-if="!(params.pageNum==1&&loadingType=='nomore')" :status="loadingType"></uni-load-more>
		</scroll-view>
	</view>
</template>
<script>
	import uniLoadMore from '@/components/uni-load-more/uni-load-more.vue'
	import hospitalCard from '@/components/card/hospital-card.vue'
	import { mapState } from 'vuex';
	export default {
		components: {
			uniLoadMore,
			hospitalCard, // 猜你喜欢 - 商品卡片
		},
		options: { styleIsolation: 'shared' },
		data() {
			return {
				timer:null,
				hospitalList: [],
				params:{
					pageNum: 1,
					pageSize: 10
				},
				isLoad: false,
				loadingType: 'more', //加载更多状态
				titleBar: null, //导航高度
				bannerList:[], // 轮播
			}
		},
		computed: {
			...mapState(['hasLogin','userInfo']),
			isEmpty() {
				return this.isLoad && this.hospitalList.length === 0
			}
		},
		onLoad(){
			this.titleBar = this.titleBarHeight

			wx.getSetting({
				success(res) {
					if (!res.authSetting['scope.userLocationBackground']) {
						wx.authorize({
							scope: 'scope.userLocationBackground',
							fail(res) {
								// 授权失败
								wx.showModal({
									title: '位置信息授权',
									content: '您拒绝了定位权限，将无法使用定位功能',
									confirmText: '开启授权',
									confirmColor: '#345391',
									cancelText: '仍然拒绝',
									cancelColor: '#999999',
									success: function (res) {
										if (res.confirm) {
											wx.openSetting({
												success (res) {
													res.authSetting = {
														"scope.userInfo": true,
														"scope.userLocation": true
													}
												}
											})
										} else if (res.cancel) {
											wx.showModal({
												title: '签到失败',
												content: '无法使用定位权限',
												confirmText: '太遗憾了',
												confirmColor: '#345391',
												showCancel: false
											})
										}
									}
								})
							}
						})
					}
				}
			})


			this.getBanner()
			this.getGoodList()
		},
		onShow(){

			getApp().globalData.orderRefresh = true // 是否刷新
			
			if(this.hasLogin) {
				wx.startLocationUpdateBackground({
					success: (res) => {
						wx.onLocationChange((_res) => {
							console.log("wx.onLocationChange")
							let params = {latitude:_res.latitude,longitude:_res.longitude}
							if(this.userInfo.latitude!=_res.latitude&&this.userInfo.longitude!=_res.longitude){

								setTimeout(() => {
									this.$apis.postApi('/user/update',params).then(({code}) => {
										if(code==200){
											console.log('地址保存成功'+JSON.stringify(params))
										}
									})
								}, 6000);//60秒
							}
						})
					}
				})
				this.timer = setInterval(() => { // 轮询
					setTimeout(() => {
					
					},0)
				},3000)
			} else {
				wx.stopLocationUpdate()
			}

		},
		onHide(){
			clearInterval(this.timer)
		},

		methods: {
			//加载商品 ，带下拉刷新和上滑加载
			async loadData(type='add') {
				if(type=='refresh') { // 初始化刷新
					this.isLoad = false
					this.params.pageNum = 1
					this.loadingType = 'more'
					this.hospitalList = []
				}
				//没有更多直接返回
				if (this.loadingType==='loading' || this.loadingType==='nomore') return
				this.getGoodList()
				if(type === 'refresh'){
					uni.stopPullDownRefresh();
				}
			},
			async getGoodList() { // 猜你喜欢
				this.loadingType = 'loading'
				const { code,data } = await this.$apis.postApi('/hospital/list', this.params)
				this.isLoad = true
				if(code==200) {
					const { list,navigateLastPage,pageNum } = data
					this.hospitalList = this.hospitalList.concat(list);
					//判断是否还有下一页，有是more  没有是nomore(测试数据判断大于20就没有了)
					this.loadingType = pageNum >= navigateLastPage ? 'nomore' : 'more';
					this.params.pageNum++
				} else {
					this.loadingType = 'nomore'
					this.hospitalList = []
				}
			},
			async getBanner() {
				const { code,data } = await this.$apis.postApi('/banner/list',{})
				if(code==200){
					this.bannerList = data.list
				}
			},
			async goAccompany() {
				const params = {
					
				}
				uni.navigateTo({
					url: '/userPages/onlineOrder?params='+encodeURIComponent(JSON.stringify(params))
				})
			},

		}
	}
</script>
<style lang="scss" scoped>
@import "./index.scss";
</style>
